package com.seanLab.tool;

import com.google.gson.Gson;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.seanLab.configuration.ModelConfig;
import com.seanLab.domain.TagInfo;
import com.seanLab.dto.RecommendArticleDto;
import com.seanLab.dto.SuggestArticleKeywordsDto;
import com.seanLab.dto.SuggestModelImageDto;
import com.seanLab.dto.SuggestModelArticleDto;
import com.seanLab.tool.TagSuggestModel.ExpandRank.common.AnsjSegment;
import com.seanLab.tool.TagSuggestModel.StaticModelProperties;
import org.ansj.domain.Term;
import org.ansj.splitWord.Analysis;
import org.ansj.splitWord.analysis.DicAnalysis;
import org.ansj.splitWord.analysis.NlpAnalysis;
import org.ansj.splitWord.analysis.ToAnalysis;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
//import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
//import org.thunlp.language.chinese.CRFWordSegment;
//import org.thunlp.language.chinese.ForwardMaxWordSegment;
import org.thunlp.language.chinese.WordSegment;

import java.io.*;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

//import static com.seanLab.tool.TagSuggestModel.StaticModelProperties.SegementModelPath;
@Deprecated
public class TestTagSuggestor {
    private static final Type POST_TYPE = new TypeToken<List<SuggestModelArticleDto>>() {}.getType();
    private static final String DATASET_PATH = "workingDir";
//    private static final String DATASET = "sina.article_xiaobo2.json";
    private static final String DATASET_ARTICLE = "chens.article_2243.json";
    private static final String DATASET_IMAGE = "chens.validImage_2243.json";
    private static final String TestArticle = "workingDir" + File.separator + "TestArticle.txt";
    private static final String TestOutput = "workingDir" + File.separator + "TestOutput.txt";

    @Autowired
    private ModelConfig modelConfig;

    List<SuggestModelArticleDto> dataset;
    TagSuggestor tagSuggestor = new TagSuggestor();

    private void loadDataset() throws IOException{
//        imageDataset = new Gson().fromJson(
//                new JsonReader(new FileReader(DATASET_PATH + File.separator + DATASET)), POST_TYPE);
        dataset = new Gson().fromJson(
                new JsonReader(new FileReader(DATASET_PATH + File.separator + DATASET_ARTICLE)), POST_TYPE);
        List<SuggestModelImageDto> images = new Gson().fromJson(
                new JsonReader(new FileReader(DATASET_PATH + File.separator + DATASET_IMAGE)),
                new TypeToken<List<SuggestModelImageDto>>() {}.getType());
        dataset.get(0).setSuggestModelImageDtoList(images);
    }

    private void testTrain() throws IOException{
        System.out.println(tagSuggestor.train(dataset));
    }

    private void testLoad() {
        System.out.print("Load Model... ");
        System.out.println(tagSuggestor.loadModel("workingDir" + File.separator + "model_22029"));
    }

    private void testTag(int articleID) {
        SuggestModelArticleDto article = dataset.get(articleID);
        System.out.println(article.getContent());
//        ArrayList<SuggestModelImageDto> images = new ArrayList<>();
//        images.add(new SuggestModelImageDto());
//        images.add(new SuggestModelImageDto());
//        article.setSuggestModelImageDtoList(images);
        List<List<TagInfo>> tagsList = tagSuggestor.doTag(article);
        for (List<TagInfo> tags : tagsList) {
            System.out.println("!!!! SuggestModelImageDto " + tagsList.indexOf(tags) + " !!!!");
            int cnt = 0;
            for (TagInfo tag : tags) {
                System.out.println(tag.getTagName() + " " + tag.getTagScore() + " " + tag.getTagSource());
                if (++cnt > 10) break;
            }
            System.out.println();
        }

    }

    private void testTag() {
        testTag(0);
    }

    private void testSuggest(int articleID) {
        RecommendArticleDto article = new RecommendArticleDto();
        article.setTitle(dataset.get(articleID).getTitle());
        article.setContent(dataset.get(articleID).getContent());
        System.out.println(article.getContent());
        long startTime = System.currentTimeMillis();
        List<SuggestArticleKeywordsDto> tags = tagSuggestor.doSuggest(article);
        long endTime = System.currentTimeMillis();
        System.out.println("Time: " + String.valueOf(endTime - startTime));
        int cnt = 0;
        for (SuggestArticleKeywordsDto tag : tags) {
            System.out.println(tag.getKeywords() + " " + tag.getScore());
            if (++cnt > 10) break;
        }
    }

    private void testSuggest() throws IOException {
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(
                        new FileInputStream(
                                TestArticle),
                        "UTF-8"));

        String title = reader.readLine();
        String content = reader.readLine();

        RecommendArticleDto p = new RecommendArticleDto();
        p.setTitle(title);
        p.setContent(content);

        long startTime = System.currentTimeMillis();
        List<SuggestArticleKeywordsDto> tags = tagSuggestor.doSuggest(p);
        long endTime = System.currentTimeMillis();
        System.out.println("Time: " + String.valueOf(endTime - startTime));

        BufferedWriter outTag = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(TestOutput),
                "UTF-8"));
        int cnt =  0;
        for (SuggestArticleKeywordsDto ws : tags) {
            outTag.write(ws.getKeywords() + " " + ws.getScore());
            System.out.println(ws.getKeywords() + " " + ws.getScore());
            ++ cnt;
            if (cnt>10) break;
            outTag.newLine();
            outTag.flush();
        }
    }

    private void suggesetDemo() throws IOException {

        List<RecommendArticleDto> dataset = new Gson().fromJson(
                new JsonReader(new FileReader("workingDir" + File.separator + "testSet.article_11061629.json")),
                new TypeToken<List<RecommendArticleDto>>() {}.getType());
//        List<RecommendArticleDto> otherDataset = new Gson().fromJson(
//                new JsonReader(new FileReader("workingDir" + File.separator + "sogou.json")),
//                new TypeToken<List<RecommendArticleDto>>() {}.getType());
//        List<RecommendArticleDto> articles = new ArrayList<>();
//        List<List<SuggestArticleKeywordsDto>> keywords = new ArrayList<>();
//        List<Report> reports = new ArrayList<>();
//        Random random = new Random(System.currentTimeMillis());
//        long startTime = System.currentTimeMillis();
//        for (int i = 0; i < 10; i++) {
//            RecommendArticleDto article = imageDataset.get(random.nextInt(imageDataset.size()));
////            articles.add(article);
////            keywords.add(tagSuggestor.doSuggest(article));
//            long startTime1 = System.currentTimeMillis();
//            reports.add(new Report(article.getContent(), tagSuggestor.doSuggest(article)));
//            long endTime1 = System.currentTimeMillis();
//            System.out.println("Time: " + String.valueOf(endTime1 - startTime1));
////            System.out.println(i);
//        }
//        for (int i = 0; i < 10; i++) {
//            RecommendArticleDto article = otherDataset.get(random.nextInt(otherDataset.size()));
////            articles.add(article);
////            keywords.add(tagSuggestor.doSuggest(article));
//            long startTime1 = System.currentTimeMillis();
//            reports.add(new Report(article.getContent(), tagSuggestor.doSuggest(article)));
//            long endTime1 = System.currentTimeMillis();
//            System.out.println("Time: " + String.valueOf(endTime1 - startTime1));
////            System.out.println(i);
//        }
//        long endTime = System.currentTimeMillis();
//        System.out.println("Time: " + String.valueOf(endTime - startTime));
//        new Gson().toJson(reports, new FileWriter("workingDir" + File.separator + "reports.json"));
//        List<Report> reportList = new Gson().fromJson(
//                new JsonReader(new FileReader("workingDir" + File.separator + "reports.json")),
//                new TypeToken<List<Report>>() {}.getType());
//        System.out.println(new Gson().toJson(reports));

        PrintWriter pw = new PrintWriter(new File("workingDir" + File.separator + "demo100.csv"));
        StringBuilder sb = new StringBuilder();
        sb.append("文章序号").append(",").append("content").append(",").append("session_ID").append(",")
                .append("提取的关键词").append(",").append("结果是否为空").append(",").append("是否采纳").append(",")
                .append("采纳的图片序号").append(",").append("采纳图片（地址）").append(",").append("查询人").append(",")
                .append("查询时间").append("\n");
//        sb.append("content");
//        sb.append(',');
//        sb.append("key1");
//        sb.append(',');
//        sb.append("key2");
//        sb.append(',');
//        sb.append("key3");
//        sb.append(',');
//        sb.append("key4");
//        sb.append(',');
//        sb.append("key5");
//        sb.append('\n');
        Random random = new Random(System.currentTimeMillis());
        for (int i = 0; i < 100; i++) {
            RecommendArticleDto article = dataset.get(random.nextInt(dataset.size()));
            sb.append(i+1).append(",");
            sb.append(article.getContent().replaceAll("\n", " ").replaceAll(",", "，"))
                    .append(',');
//            for (SuggestArticleKeywordsDto keywordsDto : report.getKeywords()) {
//                sb.append(',');
//                sb.append(keywordsDto.getKeywords() + " : " + String.valueOf(keywordsDto.getScore()));
//            }
            sb.append('\n');
        }
        pw.write(sb.toString());
        pw.close();
        System.out.println("done!");
    }

    public void testSegment() throws IOException{
//        List<RecommendArticleDto> imageDataset = new Gson().fromJson(
//                new JsonReader(new FileReader("workingDir" + File.separator + "testSet.article_11061629.json")),
//                new TypeToken<List<RecommendArticleDto>>() {}.getType());
//        Random random = new Random(127);
//        RecommendArticleDto article = imageDataset.get(random.nextInt(imageDataset.size()));
        RecommendArticleDto article = new RecommendArticleDto();
        BufferedReader reader = new BufferedReader(new FileReader("workingDir" + File.separator + "TestArticle.txt"));
        article.setTitle(reader.readLine());
        article.setContent(reader.readLine());
        reader.close();

        WordSegment analysis = new AnsjSegment();
        System.out.println("Start");
        long startTime = System.currentTimeMillis();
        System.out.println(String.join(" ", analysis.segment(article.getContent())));
        long endTime = System.currentTimeMillis();
        System.out.println(DicAnalysis.parse(article.getContent()));
        System.out.println("Time: " + String.valueOf(endTime - startTime));
        System.out.println();

//        article = imageDataset.get(random.nextInt(imageDataset.size()));

        startTime = System.currentTimeMillis();
        System.out.println(String.join(" ", analysis.segment(article.getContent())));
        endTime = System.currentTimeMillis();
        System.out.println("Time: " + String.valueOf(endTime - startTime));
        System.out.println();

//        System.setProperty("wordsegment.automata.file", SegementModelPath);
        WordSegment forwadSeg = new AnsjSegment();
        startTime = System.currentTimeMillis();
        System.out.println(String.join(" ", forwadSeg.segment(article.getContent())));
        endTime = System.currentTimeMillis();
        System.out.println("Time: " + String.valueOf(endTime - startTime));
        System.out.println();

        System.out.println(article.getContent());
    }

    class DoubleList{
        public DoubleList(List<Double> values) {
            this.values = values;
        }

        public List<Double> getValues() {
            return values;
        }

        public void setValues(List<Double> values) {
            this.values = values;
        }

        List<Double> values;
    }

    private void testTopic() throws IOException{
        String content = "马来西亚羽球双打名将林钦华和炒鸡蛋，已经确定从9月开始，与韩国双打名将柳延星组成搭档，出战国际赛。28岁的前国手林钦华日前向媒体透露这项消息时证实，他和柳延星（31岁）合作的第一个比赛，是韩国公开赛。 　　林钦华与柳延星会擦出怎样的火花，令人非常期待，他俩也是继大马的陈文宏与印尼的亨德拉以后，另一对跨国男双羽球组合。 　　不同的是，陈文宏与亨德拉是大马和印尼，林钦华与柳延星是大马和韩国。林钦华出道以来，与多名好手组成拍档，包括麦喜俊、吴蔚升、陈炳顺、王建国和云天豪，也曾与黄惠龄、黄佩蒂、林芸如等打过混双，2015年正式离开国家队后，选择以自由人身分征战国际赛。  　　柳延星也是世界羽坛双打名将，既打混双也打男双，尤其与李龙大搭档，更是韩国男双一个时代的标杆；去年里约奥运会后，李龙大和柳延星先后退出国家队。级别为超级系列赛的韩国公开赛，将于9月12日至17日举行，总奖金高达60万美元，同样是超级系列赛的日本公开赛，将于19日至24日上演，总奖金有32万5000美元。打完韩国公开赛以后，“钦星”（清新）组合也会参加日本公开赛。  　　紫盟联赛撮合 　　林钦华坦承，类似异国搭档，最困难的是因为分隔两地，以致给2人的训练造成一定的困难，为此，他计划在9月初启程韩国与柳延星会合，以便在当地展开训练直至上场比赛，争取时间培养默契和磨合。 　　这次与柳延星搭档，林钦华本人将在著名羽球品牌apacs旗下参赛，他透露，与柳延星的“结盟”，源自于今年紫盟联赛。当时2人都代表蒲种联合队。  　　紫盟期间，他跟延星分享这个合作的心愿，得到正面回应，于是，两人在敲定以后就各自处理细节。此前，爱羽客小编也曾在一项业余赛事中也询问过林钦华退役后的打算，详情请戳：吴蔚昇前搭档自爆：在国家队压力很大 　　（注：林钦华，1989年，马来西亚男子羽毛球运动员；曾经搭档吴蔚昇在2014年大马首要超级赛上击败柴飚/洪炜，首次夺得超级系列赛冠军头衔。2015年2月，林钦华正式递交辞职信，申请离开马来西亚国家队。） 　　来源：中国报\n";
//        String content = "新华社上海1月18日电（记者桑彤）针对近期市场热炒“区块链”概念，上海证券交易所近日指出，相关公司股价出现连续上涨，涉及沪市10家公司，个别股票已经出现炒作风险。对此，上交所相关监管部门高度重视，并第一时间组织分析研判。  上交所表示，“区块链”技术仍处于开发阶段，尚难以形成稳定业务，概念炒作迹象比较明显。对此，上交所对相关概念股采取停牌问询、停牌冷却、澄清说明等分类监管措施。主要涉及以下四类情况：  第一类，公司前期已披露开展“区块链”业务，近几日股价出现连续上涨，成为龙头概念。典型如易见股份，股价连续四天涨停，累计涨幅达46%。针对该情况，上交所对公司股票实施停牌冷却处理，并要求公司详细披露“区块链”业务开展情况，并充分提示风险。  第二类，涉嫌主动“贴热点”，自行发布公司业务范围涉及“区块链”，典型为游久游戏、商赢环球。对此，上交所在监测到公司相关言论后，对公司股票采取晨间紧急停牌，并向公司发出问询函，要求公司详细说明是否迎合市场热点、相关行为是否违规，并充分提示风险，同时核查内幕交易。据悉，上交所后续还将对公司涉嫌违规行为采取纪律处分措施。  第三类，因业务与“区块链”有某种联系，被市场归为“区块链”概念股，近期也有一定涨幅，包括金证股份、信雅达、恒银金融等。对此，上交所已要求公司统一发布澄清公告，重点是澄清公司业务与“区块链”技术并无直接关联，公司也尚未开展“区块链”业务。  第四类，属于“区块链”边缘概念股，不涉及“区块链”业务，但因主营业务涉及互联网、软件等，股价也出现一定涨幅，但已趋于平稳，包括浙大网新、用友网络、恒生电子。对此类公司，上交所表示，保持密切关注，并将结合股价后续走势，视情况要求公司澄清。  上交所相关部门表示，后续将继续密切关注股价走势，做好持续监管工作。";
////        tagSuggestor.loadLdaModel("file://workingDir/LDAModel");
////        List<Double> resutl = tagSuggestor.doTopics(content);
////        System.out.println(resutl);
//        HttpClient httpclient = HttpClients.createDefault();
////        HttpPost httppost = new HttpPost("http://10.141.211.97:9347/suggestTopic");
//        HttpPost httppost = new HttpPost("http://localhost:9347/suggestTopic");
////        HttpEntity entity = new ByteArrayEntity(content.getBytes("UTF-8"));
//        HttpEntity entity = new StringEntity(content,"UTF-8");
//        System.out.println(new BufferedReader(new InputStreamReader(entity.getContent())).readLine());
//        httppost.setEntity(entity);
//        long start = System.currentTimeMillis();
//        HttpResponse response = httpclient.execute(httppost);
//        long end = System.currentTimeMillis();
//        String result = EntityUtils.toString(response.getEntity());
//        System.out.println(result);
//        DoubleList doubleList = new Gson().fromJson(result, DoubleList.class);
//        System.out.println(doubleList.values);
//        System.out.println(end - start);
        this.tagSuggestor.loadLdaModel(modelConfig.getLdaModelPath());
        System.out.println(tagSuggestor.doTopics(content));
        System.out.println(tagSuggestor.doTopics(content));
        System.out.println(tagSuggestor.doTopics(content));
        System.out.println(tagSuggestor.doTopics(content));
    }

    public static void main(String[] args) throws IOException{
        TestTagSuggestor test = new TestTagSuggestor();
        test.testSegment();
//        test.suggesetDemo();
//        test.loadDataset();
//        test.testTrain();
//        test.testLoad();
//        test.testTopic();
//        test.testTag();
//        test.testSuggest();
//        test.testSuggest(0);
    }
}
